package graph


func GenerateGraph(tableGraph [][]interface{}) Graph {
	nodeMap := map[interface{}]*Node{}
	edgesSet := map[*Edge]bool{}
	for _, v := range tableGraph {
		weight := v[0]
		from := v[1]
		to := v[2]
		var fromNode *Node
		var toNode *Node
		if _, ok := nodeMap[from]; ok {
			fromNode = nodeMap[from]
		} else {
			fromNode = &Node{Val: from}
			nodeMap[from] = fromNode
		}
		if _, ok := nodeMap[to]; ok {
			toNode = nodeMap[to]
		} else {
			toNode = &Node{Val: to}
			nodeMap[to] = toNode
		}
		edge := &Edge{From: fromNode, To: toNode, Weight: weight.(int)}
		fromNode.Out++
		fromNode.NextS = append(fromNode.NextS, toNode)
		fromNode.OutEdges = append(fromNode.OutEdges, edge)
		toNode.In++
		edgesSet[edge] = true
	}
	return Graph{nodeMap, edgesSet}
}
